package com.twl.app.base.bo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.twl.app.base.entity.CheckDate;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Companyb;
import com.twl.app.base.entity.Miniwase;
import com.twl.app.base.entity.SalParam;
import com.twl.app.base.entity.SalSalaryData;
import com.twl.app.base.entity.SalSocialAdjust;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.CryptUtils;
import com.twl.app.base.util.NotNullBeanUtils;
import com.twl.common.constant.SalConstant;
import com.twl.core.framework.bo.impl.AbsCrudBoImpl;
import com.twl.core.framework.dao.WSHibernateDaoSupportProxy;
import com.twl.core.framework.page.ExpResult;
import com.twl.core.framework.page.Page;
import com.twl.core.framework.page.Result;
@Transactional
@Repository("salaryBoImpoty")
public class SalaryBoImpoty extends AbsCrudBoImpl {
	
	public Logger log = Logger.getLogger(SalaryBoImpoty.class);
	
	@Autowired
	private WSHibernateDaoSupportProxy wSHibernateDaoSupportProxy;
	

	
	/**
	 * 更新出勤日期
	 * @param date
	 * @return
	 */
	public String updateDate(String sql){
		try{
			//String sql = "delete checkdate where workdate = '"+date+"' ";
			int num =super.executeSql(sql);
		}catch (Exception e) {
			e.printStackTrace();
			return "false" ;
		}
		return "ok" ;
	}
	/***
	   * 查询出勤日期
	   * @param sql
	   * @param page
	   * @return
	   */
	public Result queryDate(String sql,Page page) {
		 Result result=wSHibernateDaoSupportProxy.getListByPage(sql, page);
		return result;
	}
	/**
	 * 增加出勤表
	 * @return
	 */
	public String insertDate(CheckDate checkDate){
		
		try {
			super.addData(checkDate);
		} catch (Exception e) {
			e.printStackTrace();
			return "false" ;

		}
		
		return "ok" ;
	}
	
	/***
	 * 增加考勤表
	 * 
	 * @param let
	 * @return
	 */
	public String savechecktemplet(Checktemplet let) {
		Object fr = null;
		try {
			String sql = "from Checktemplet where staffint='" + let.getStaffint()
					+ "' and period='" + let.getPeriod() + "' or idNumber='"+let.getIdNumber()+"'";
			Checktemplet dbResult = (Checktemplet) wSHibernateDaoSupportProxy
					.uniqueResult(sql);
			if (dbResult!= null) {
				NotNullBeanUtils.copyProperties(dbResult, let);
				wSHibernateDaoSupportProxy.saveOrUpdate(dbResult);
			} else {
				fr=super.addData(let);
			}

			//如果为新增
			if(let.getDid()==null){
				//覆盖更新功能
				String period = let.getPeriod();// 期间
				String staffint = let.getStaffint(); // 工号
				
				if(StringUtils.isNotBlank(period)&&StringUtils.isNotBlank(staffint)){
					overrideChecktemplet(period,staffint);
				}
			}
			
			fr = super.saveOrUpdateData(let);
		} catch (Exception e) {
			log.fatal("---------------增加出错");
			e.printStackTrace();
		}
		if (fr != null) {
			return "YES";
		} else {
			return "NO";
		}

	}
	

	/**
	* 覆盖删除考勤记录
	* @param ids
	* @return
	*/
	public Integer overrideChecktemplet(String period, String staffint) {
		StringBuffer hql = new StringBuffer("delete from Checktemplet where 1=1 ");
		if (StringUtils.isNotBlank(period)) {
			hql.append(" and period ='" + period + "' ");
		}
		if (StringUtils.isNotBlank(staffint)) {
			hql.append(" and staffint ='" + staffint + "' ");
		}
		return wSHibernateDaoSupportProxy.executeUpdate(hql.toString());

	}
	
	
	/***
	 * 导出列表(所有带分页的导出都可以调用这一个)
	 * @param sql
	 * @param page
	 * @return
	 */
	public ExpResult getExpResult(String sql,Page page){
		
		ExpResult expResult=wSHibernateDaoSupportProxy.getExpListByPage(sql, page);
		return expResult;
	}
	
	
	/**
	 * 批量删除考勤记录
	 * @param ids
	 */
	public Integer batchDelChecktemplet(Integer [] ids){
		String hql = "delete from Checktemplet where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	}
	

	/**
	 * 增加员工
	 * 
	 * @param emp
	 * @return
	 */
	public String saveuemp(Uemp emp) {
		Object fr = null;
		
		try {
			//加密
		    Uemp uemp =	CryptUtils.encrypt(emp);
			fr = super.addData(uemp);
		} catch (Exception e) {
			log.fatal("---------------增加出错");
			e.printStackTrace();
		}
		if (fr != null) {
			return "YES";
		} else {
			return "NO";
		}
	}

	/**
	 * 增加社保
	 * 
	 * @param com
	 * @return
	 */
	public String savecompanyb(Companyb com) {
		Object fr = null;
		try {
			fr = super.addData(com);
		} catch (Exception e) {
			log.fatal("---------------增加出错");
			e.printStackTrace();
		}
		if (fr != null) {
			return "YES";
		} else {
			return "NO";
		}
	}

	/***
	 * 修改员工
	 * 
	 * @param sql
	 * @return
	 */
	public int updateuemp(String sql) {
		int num = super.executeSql(sql);
		return num;
	}
	/***
	 * 删除员工
	 * 
	 * @param sql
	 * @return
	 */
	public int deluemp(String sql) {
		int num = super.executeSql(sql);
		return num;
	}
	
	
	/**
	 * 根据主键逻辑删除薪资数据
	 * @param ids
	 * @return
	 */
	public Integer batchDelUemp(Integer [] ids){
		String hql = "update Uemp set delstaff ='"+SalConstant.IS_DELTE+"' where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	
	}
	
	
	/***
	 * 修改社保
	 * @param sql
	 * @return
	 */
	public int updatecomp(String sql){
		int num = super.executeSql(sql);
		return num;
	}
   /***
    * 查询员工信息web
    * @param sql
    * @param params
    * @return
    */
	public List<Uemp> selectuemp(String sql) {
		@SuppressWarnings("unchecked")
		List<Uemp> list = super.getListPage(sql);
		List<Uemp> uemplist=new ArrayList<Uemp>();
		try {
			if(list.size()>0){
				uemplist= CryptUtils.decryptNew(list);
			}else{
				return list;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return uemplist;
	}
	public Result selectuempstr(String sql,Page page){
		 Result ult=wSHibernateDaoSupportProxy.getListByPage(sql, page);
		 return ult;
	}
	/***
	 * 查询考勤
	 * @param sql
	 * @param page
	 * @return
	 */
	public Result selectchecktemplet(String sql,Page page){
		
	  Result ult=wSHibernateDaoSupportProxy.getListByPage(sql, page);
		return ult;
	}
	  /***
	   * 查询社保
	   * @param sql
	   * @param page
	   * @return
	   */
	public Result selectcomp(String sql,Page page ){
		  Result ult=wSHibernateDaoSupportProxy.getListByPage(sql, page);
		  return ult;
	}
	
	public int delcomp(String id){
		String sql="delete Companyb where did='"+id+"'";
		int num=super.executeSql(sql);
		return num;
	}
	
	/**
	 * 批量删除社保
	 * @param ids
	 */
	public Integer batchDelCompanyb(Integer [] ids){
		String hql = "delete from Companyb where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	}
	/**
	 * 批量更改出勤日期
	 * @param ids
	 */
	public Integer updateWorkdays(Integer [] ids){
		String hql = "update from CheckDate set iswork ='1' where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	}
	public Integer updateWeekdays(Integer [] ids){
		String hql = "update from CheckDate set iswork ='0' where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	}
	/**
	 * 保存或者更新薪资数据
	 * @param data
	 * @return
	 */
	public Integer saveSalSalaryData(SalSalaryData data) {
		SalSalaryData dbResult = new SalSalaryData();
		if(data.getDid()!=null){
			 dbResult = (SalSalaryData) wSHibernateDaoSupportProxy.get(SalSalaryData.class, data.getDid());
		}else{
			//覆盖更新功能
			String period = data.getPeriod();// 期间
			String staffint = data.getStaffint(); // 工号
			//String idNumber = data.getIdNumber(); // 身份证号
			
			if(StringUtils.isNotBlank(staffint)&&StringUtils.isNotBlank(period)){
				overrideSalSalaryData(period,staffint,null);
			}
			
		}
		//不拷贝空值
		NotNullBeanUtils.copyProperties(dbResult, data);
		dbResult.setIsDelete(SalConstant.NOT_DELETE);
		wSHibernateDaoSupportProxy.saveOrUpdate(dbResult);
		return 1;
	}
	
	
	
	/**
	 * 覆盖更新原来的记录(逻辑删除掉)
	 * @param ids
	 * @return
	 */
	public Integer overrideSalSalaryData(String period,String staffint,String idNumber){
		StringBuffer hql = new StringBuffer("update SalSalaryData set isDelete ='"+SalConstant.IS_DELTE+"' where isDelete ='"+SalConstant.NOT_DELETE+"'");
		if(StringUtils.isNotBlank(period)){
			hql.append(" and period ='"+period+"' ");
		}
		if(StringUtils.isNotBlank(staffint)){
			hql.append(" and staffint ='"+staffint+"' ");
		}
		if(StringUtils.isNotBlank(idNumber)){
			hql.append(" and idNumber ='"+idNumber+"' ");
		}
		return wSHibernateDaoSupportProxy.executeUpdate(hql.toString());
	
	}
	
	
	/**
	 * 根据主键逻辑删除薪资数据
	 * @param ids
	 * @return
	 */
	public Integer delSalSalaryData(Integer [] ids){
		String hql = "update SalSalaryData set isDelete ='"+SalConstant.IS_DELTE+"' where did in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	
	}
	
	/**
	 * 查询薪资数据
	 * @param hql
	 * @param page
	 * @return
	 */
	public Result querySalaryData(String hql,Page page){
		Result result=wSHibernateDaoSupportProxy.getListByPage(hql, page);
		return result;
	}
	
	
	/**
	 * 保存或者更新社保调整
	 * @param data
	 * @return
	 */
	public Integer saveSalSocialAdjust(SalSocialAdjust data) {
		SalSocialAdjust dbResult = new SalSocialAdjust();
		if(data.getId()!=null){
			 dbResult = (SalSocialAdjust) wSHibernateDaoSupportProxy.get(SalSocialAdjust.class, data.getId()); 
		}else{
			//覆盖更新功能
			String period = data.getPeriod();// 期间
			String staffint = data.getStaffint(); // 工号
			//String idNumber = data.getIdNumber(); // 身份证号
			
			if(StringUtils.isNotBlank(period)&&StringUtils.isNotBlank(staffint)){
				overrideSalSocialAdjust(period,staffint,null);
			}
			
		}
		//不拷贝空值
		NotNullBeanUtils.copyProperties(dbResult, data);
		dbResult.setIsDelete(SalConstant.NOT_DELETE);
		wSHibernateDaoSupportProxy.saveOrUpdate(dbResult);
		return 1;
	}
	
	
	
	/**
	 * 覆盖更新原来的社保调整记录(逻辑删除掉)
	 * @param ids
	 * @return
	 */
	public Integer overrideSalSocialAdjust(String period,String staffint,String idNumber){
		StringBuffer hql = new StringBuffer("update SalSocialAdjust set isDelete ='"+SalConstant.IS_DELTE+"' where isDelete ='"+SalConstant.NOT_DELETE+"'");
		if(StringUtils.isNotBlank(period)){
			hql.append(" and period ='"+period+"' ");
		}
		if(StringUtils.isNotBlank(staffint)){
			hql.append(" and staffint ='"+staffint+"' ");
		}
		if(StringUtils.isNotBlank(idNumber)){
			hql.append(" and idNumber ='"+idNumber+"' ");
		}
		return wSHibernateDaoSupportProxy.executeUpdate(hql.toString());
	
	}
	
	
	/**
	 * 根据主键逻辑删除社保调整
	 * @param ids
	 * @return
	 */
	public Integer delSalSocialAdjust(Integer [] ids){
		String hql = "update SalSocialAdjust set isDelete ='"+SalConstant.IS_DELTE+"' where id in (:ids)";
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	
	}
	
	/**
	 * 查询社保调整
	 * @param hql
	 * @param page
	 * @return
	 */
	public Result querySalSocialAdjust(String hql,Page page){
		Result result=wSHibernateDaoSupportProxy.getListByPage(hql, page);
		return result;
	}

	/**
	 * 保存或者更新薪资系统参数
	 * @param data
	 * @return
	 */
	public Integer saveSalParam(SalParam data) {
		SalParam dbResult = new SalParam();
		if(data.getParamId()!=null){
			 dbResult = (SalParam) wSHibernateDaoSupportProxy.get(SalParam.class, data.getParamId()); 
		}else{
			//覆盖更新功能
			String paramType = data.getParamType();
			String paramKey = data.getParamKey();
			
			if(StringUtils.isNotBlank(paramType)&&StringUtils.isNotBlank(paramKey)){
				overrideSalParam(paramType,paramKey);
			}
		}
		//不拷贝空值
		NotNullBeanUtils.copyProperties(dbResult, data);
		dbResult.setIsDelete(SalConstant.NOT_DELETE);
		wSHibernateDaoSupportProxy.saveOrUpdate(dbResult);
		return 1;
	}
	
	
	
	/**
	 * 覆盖更新原来的薪资系统参数记录(逻辑删除掉)
	 * @param ids
	 * @return
	 */
	public Integer overrideSalParam(String paramType,String paramKey){
		StringBuffer hql = new StringBuffer("update SalParam set isDelete ='"+SalConstant.IS_DELTE+"' where isDelete ='"+SalConstant.NOT_DELETE+"'");
		if(StringUtils.isNotBlank(paramType)){
			hql.append(" and paramType ='"+paramType+"' ");
		}
		if(StringUtils.isNotBlank(paramKey)){
			hql.append(" and paramKey ='"+paramKey+"' ");
		}
		return wSHibernateDaoSupportProxy.executeUpdate(hql.toString());
	
	}
	
	
	/**
	 * 根据主键逻辑删除薪资系统参数
	 * @param ids
	 * @return
	 */
	public Integer delSalParam(Integer [] ids){
		String hql = "update SalParam set isDelete ='"+SalConstant.IS_DELTE+"' where paramId in (:ids)";   
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	
	}
	
	/**
	 * 查询薪资系统参数
	 * @param hql
	 * @param page
	 * @return
	 */
	public Result querySalParam(String hql,Page page){
		Result result=wSHibernateDaoSupportProxy.getListByPage(hql, page);
		return result;
	}

	
	
	
	
	
	
	/**
	 * 保存或者更新最低工资参数
	 * @param data
	 * @return
	 */
	public Integer saveMiniwase(Miniwase data) {
		Miniwase dbResult = new Miniwase();
		if(data.getDid()!=null){
			 dbResult = (Miniwase) wSHibernateDaoSupportProxy.get(Miniwase.class, data.getDid()); 
		}else{
			//覆盖更新功能
			String engtype = data.getEngtype();// 城市英文
			String vmoney = data.getVmoney();//  标识
			
			if(StringUtils.isNotBlank(engtype)&&StringUtils.isNotBlank(vmoney)){
				overrideMiniwase(engtype,vmoney);
			}
			
		}
		//不拷贝空值
		NotNullBeanUtils.copyProperties(dbResult, data);
		dbResult.setIsDelete(SalConstant.NOT_DELETE);
		wSHibernateDaoSupportProxy.saveOrUpdate(dbResult);
		return 1;
	}
	
	
	
	/**
	 * 覆盖更新原来的最低工资参数记录(逻辑删除掉)
	 * @param ids
	 * @return
	 */
	public Integer overrideMiniwase(String engtype,String vmoney){
		StringBuffer hql = new StringBuffer("update Miniwase set isDelete ='"+SalConstant.IS_DELTE+"' where isDelete ='"+SalConstant.NOT_DELETE+"'");
		if(StringUtils.isNotBlank(engtype)){
			hql.append(" and engtype ='"+engtype+"' ");
		}
		if(StringUtils.isNotBlank(vmoney)){
			hql.append(" and vmoney ='"+vmoney+"' ");
		}
		return wSHibernateDaoSupportProxy.executeUpdate(hql.toString());
	
	}
	
	
	/**
	 * 根据主键逻辑删除最低工资参数
	 * @param ids
	 * @return
	 */
	public Integer delMiniwase(Integer [] ids){
		String hql = "update Miniwase set isDelete ='"+SalConstant.IS_DELTE+"' where did in (:ids)";   
		return wSHibernateDaoSupportProxy.batchDel(hql, ids);
	
	}
	
	/**
	 * 查询最低工资参数
	 * @param hql
	 * @param page
	 * @return
	 */
	public Result queryMiniwase(String hql,Page page){
		Result result=wSHibernateDaoSupportProxy.getListByPage(hql, page);
		return result;
	}
	
	
	public String getStringValue(Object obj){
		return obj==null?"":String.valueOf(obj);
	}
	
	
}
